var express = require("express");
var router = express.Router();
// var async = require("async");
const follow = require("../model/follow");
const article = require("../model/article");
let resource = require("../model/resource")
let problem = require("../model/problem")
router.post("/api/v1/getFollowArt", async (req, res) => {
  console.log(req.body);
  let followData = await follow.find(req.body.query).populate("userid");
  let userid = []
  for(let i = 0 ;i<followData.length;i++){
    userid.push(followData[i].followid)
  }
  let answerData = article.find({userid:{"$in":userid},audit: true}).populate({path: 'userid', populate: {path: 'department'}}).sort({release_time:-1})
  let resourceData = await resource.find({
    userid: {
      "$in": userid
    },
    audit: true
  }).populate({path: 'userid', populate: {path: 'department'}}).sort({
    release_time: -1
  })

  let problemData = await problem.find({
    userid: {
      "$in": userid
    },
    audit: true
  }).populate({path: 'userid', populate: {path: 'department'}}).sort({
    release_time: -1
  })

  let searchData = (await answerData).concat(resourceData, problemData)

  searchData.sort((a, b) => {
    return Date.parse(b.release_time) - Date.parse(a.release_time)
  })

  let sendData = searchData.splice(req.body.skip, req.body.limit)
  res.json({
    code: 200,
    data: sendData
  });

});

// 导出模块（在 app.js 中引入）
module.exports = router;